package com.rzjm.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.FireWorkDao;
import com.rzjm.api.dao.FireWorkMeasuresDao;
import com.rzjm.api.entity.*;
import com.rzjm.api.exception.TransactionalException;
import com.rzjm.api.service.DeviceWeldingRecordService;
import com.rzjm.api.service.DeviceWeldingService;
import com.rzjm.api.service.FireWorkMeasuresService;
import com.rzjm.api.service.WxTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class FireWorkMeasuresServiceImpl extends BaseServiceImpl<FireWorkMeasuresDao, FireWorkMeasures> implements FireWorkMeasuresService {

    @Resource
    private FireWorkMeasuresDao workMeasuresDao;
    @Resource
    private FireWorkDao fireWorkDao;
    @Resource
    private WxTemplateService wxTemplateService;
    @Resource
    private DeviceWeldingRecordService weldingRecordService;
    @Resource
    private DeviceWeldingService weldingService;

    @Override
    @Transactional
    public int checkMeasures(User user, FireWork fireWork, SysUser sysUser, List<FireWorkMeasures> workAnalysis) {
        int ret = 0;
        try{
            for (FireWorkMeasures measures : workAnalysis){
                measures.setSubmitDeptId(sysUser.getDeptId());
                measures.setSubmitSysUserId(sysUser.getUserId());
                measures.setSubmitSysUserName(sysUser.getName());
                measures.setSubmitWxUserId(sysUser.getDeptId());

                measures.setSubmitWxUserId(user.getId());
                measures.setState(1);
                measures.setSubmitTime(new Date());

                ret = workMeasuresDao.updateById(measures);
                if(ret <= 0){
                    throw new TransactionalException("workMeasuresDao insert error");
                }
            }

//            if(fireWork.getHeaderOpState() == 0 && fireWork.getHeaderSysUserId().intValue() == sysUser.getUserId().intValue()){
//                fireWork.setHeaderOpTime(new Date());
//                fireWork.setHeaderOpState(1);
//            }
            boolean isSafe = false;
            if(fireWork.getSafeOpState() == 0 && fireWork.getSafeSysUserId().intValue() == sysUser.getUserId().intValue()){
                fireWork.setSafeOpTime(new Date());
                fireWork.setSafeOpState(1);
                isSafe = true;
            }

            if(fireWork.getSafeOpState() == 1 ){
                fireWork.setState(1);
            }

            //// 1 - 非生产  2 -  生产
//            if(fireWork.getRecordType() == 0 ){
//                if(fireWork.getOpLevel() == 1){//非生产
//                    fireWork.setState(2);
//                }
//                fireWork.setLastCheckState(1);
//                fireWork.setLastCheckTime(new Date());
//                fireWork.setLastCheckResult("审核通过");
//                fireWork.setCheckSignImg(fireWork.getSafeSignImg());
//            }

            ret = fireWorkDao.updateById(fireWork);
            if(ret <= 0){
                throw new TransactionalException("fireWorkDao insert error");
            }

            if(isSafe){

                QueryWrapper<DeviceWeldingRecord> qw = new QueryWrapper<>();
                qw.eq("fire_work_id", fireWork.getId());
                qw.last("limit 1");
                DeviceWeldingRecord deviceRecord = weldingRecordService.getOne(qw);
                if(deviceRecord!= null){
                    DeviceWelding welding = weldingService.findById(deviceRecord.getDeviceId());
                    if(welding!= null){
                        wxTemplateService.sendWxMsgUseDeviceToProduceManager(deviceRecord, welding, fireWork, 0);
                    }
                }
                if (fireWork.getRecordType() == 1 || fireWork.getRecordType() == 2){
                    wxTemplateService.sendWxMsgUseFireWorkToProduceManager(fireWork);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new TransactionalException("checkMeasures system error");
        }
        return ret;
    }
}
